package ${pkgName};

<#list import_list as imports>
import ${imports};
</#list>

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.fire.code.user.entity.UserEntity;
import com.fire.common.entity.FireMsg;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fire.common.util.PageElementUtil;
import com.fire.common.util.WebConstant;
import com.fire.common.util.JsonUtil;
import com.fire.helper.base.StringHelper;


/**
 * @author close
 * @desc 
 */

@Controller
@RequestMapping(value = "${visitLocation}")
public class ${className} <#if extendStr??>extends ${extendStr}</#if> {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private ${serviceClassName} ${serviceVisitName};
	

	/**
	 * @Title: index
	 * @author: close
	 * @Description: 首页跳转
	 * @throws Exception
	 * @return String
	 * @date 2016年8月23日 上午11:07:27
	 */
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index() throws Exception {
		return "${visitLocation}/${indexVisitName}";
	}
	

	/**
	 * @Title: add
	 * @author: close
	 * @Description: 新增跳转
	 * @throws Exception
	 * @return String
	 * @throws @date
	 *             2016年8月23日 上午11:07:37
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add()throws Exception{
		return "${visitLocation}/${viewVisitName}";
	}
	
	/**
	 * @Title: modify
	 * @author: close
	 * @Description: 修改跳转
	 * @param request
	 * @param id
	 * @throws Exception
	 * @return String
	 */
	@RequestMapping(value = "/search", method = RequestMethod.GET)
	public String search(HttpServletRequest request,String id) throws Exception{
		if(id!=null && !"".equals(id)){
			${modebeanClassName} entity = this.${serviceVisitName}.search(id);
			request.setAttribute("datamap", objectToJson(entity));
		}
		return "${visitLocation}/${viewVisitName}";
	}
	
	/**
	 * @Title: listWithPage
	 * @author: close
	 * @Description: 获得grid对应的json
	 * @param request
	 * @param searchParams
	 * @throws Exception
	 * @return String
	 */
	@RequestMapping(value = "/listWithPage", method = RequestMethod.POST)
	@ResponseBody
	public String listWithPage(HttpServletRequest request) throws Exception {
		try {
			Map<Object, Object> map = PageElementUtil.toMap(request);
			
			${modebeanClassName} entity = (${modebeanClassName}) PageElementUtil.toPojo(map, ${modebeanClassName}.class);
			
			int page =Integer.valueOf(StringHelper.valueOf(map.get(WebConstant.PAGE),"0")); 
			int rows=  Integer.valueOf(StringHelper.valueOf(map.get(WebConstant.ROWS),"0"));
			
			return objectToJson(this.${serviceVisitName}.listWithPage(page,rows,entity));
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * @Title: list
	 * @author: close
	 * @Description: 获得List对应的json
	 * @param request
	 * @param searchParams
	 * @throws Exception
	 * @return String
	 */
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public String list(HttpServletRequest request) throws Exception {
		try {
			Map<Object, Object> map = PageElementUtil.toMap(request);
			${modebeanClassName} entity = (${modebeanClassName}) PageElementUtil.toPojo(map, ${modebeanClassName}.class);
			return objectToJson(this.${serviceVisitName}.list(entity));
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
			return null;
		}
	}

	
	/**
	 * @Title: save
	 * @author: close
	 * @Description: 保存修改通用方法
	 * @param request
	 * @param optName
	 * @throws Exception
	 * @return String
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public String save(HttpServletRequest request,String optName) throws Exception {
		try {
			Map<Object, Object> map = PageElementUtil.toMap(request);
			${modebeanClassName} entity = (${modebeanClassName}) PageElementUtil.toPojo(map, ${modebeanClassName}.class);
			if(StringHelper.isBlank(optName)){
				return JsonUtil.error("请指定optName标识");
			}else{

				UserEntity user = (UserEntity) request.getSession().getAttribute(WebConstant.USER);

				FireMsg<${modebeanClassName}> fireMsg = this.${serviceVisitName}.save(entity, optName, user);
				if (fireMsg.isFlag()) {
					return JsonUtil.success(StringHelper.valueOf(fireMsg.getMsg(), "保存成功！"));
				}
				return JsonUtil.error(StringHelper.valueOf(fireMsg.getMsg(), "保存失败!"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			return JsonUtil.error("保存异常");
		}
	}
	
	/**
	 * @Title: delete
	 * @author: close
	 * @Description: 删除
	 * @param id
	 * @return String
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public String delete(String... id){
		try {
			if(id!=null && !"".equals(id)){
				FireMsg<${modebeanClassName}> fireMsg = this.${serviceVisitName}.delete(Arrays.asList(id));
				if (fireMsg.isFlag()) {
					return JsonUtil.success(StringHelper.valueOf(fireMsg.getMsg(), "删除成功！"));
				}
				return JsonUtil.error(StringHelper.valueOf(fireMsg.getMsg(), "删除失败!"));				
			}
			
			return JsonUtil.success("删除成功！");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
			return JsonUtil.error("删除异常！");
		}
	}
	
}
